# -*- coding:utf-8 -*-

# -*- coding: utf-8 -*-

import traceback
from django.http.response import JsonResponse
from django.shortcuts import render
from django.conf import settings
import datetime
def get_param_by_request(request, param_name, default_val=None, _type=None):

    try:
        if param_name in request:
            _val = request[param_name]
        else:
            _val = default_val
        if _type:
            _val = _type(_val)
        return _val
    except Exception as e:
        # log.warn("get param failed, %s" % e)
        return default_val

def success_json(data={}):
    result = dict(
        error_code = 0,
        success=True,
        error_msg='',
        current_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        result=data
    )
    return JsonResponse(result)


def failed_json(code = 500, error='', data={}):
    result = dict(
        error_code = code,
        success = True,
        error_msg = error,
        current_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        result = data
    )

    return JsonResponse(result)